#include "LivingObject.h"
#include "easy2d.h"

namespace easy2d {

	LivingObject::LivingObject()
	{
		mIsFlipX = false;
		mCurDir = 2;
		mCurAction = 0;
		mCurFrame = 99999;
		mLastTick = 0;
	}

	LivingObject::~LivingObject()
	{
		release();
	}

	void LivingObject::release()
	{
		
		
	}

	
	bool LivingObject::load(const char* filename)
	{
		using namespace pugi;

		release();

#if 1
		mActionList.resize(4);
		mActionList[mCurAction].partList[0].load("assets/character/010120004.plist");
		mActionList[mCurAction].partList[1].load("assets/character/020120004.plist");
		mActionList[mCurAction].partList[2].load("assets/character/030120004.plist");
		mActionList[mCurAction].partList[3].load("assets/character/040120004.plist");
		
#endif

		//mActionList.resize(1);
		//mActionList[mCurAction].partList[0].load("assets/npc/021003007.plist");
	

		return true;
	}

	void LivingObject::update()
	{
		if (mActionList.empty())
			return;

		uint curTick = Platform::clock();

		int frameNum = mActionList[mCurAction].partList[1].getFrameCount() / 3;

		if (curTick - mLastTick > 150) {
			mCurFrame++;
			if (mCurFrame >= (mCurDir+1)*frameNum)
				mCurFrame = (mCurDir+1)*frameNum - frameNum;

			mLastTick = curTick;

			//LOGI("Frame: %d", mCurFrame);
		}
	}

	void LivingObject::render(int x, int y)
	{
		if (mActionList.empty())
			return;

		for (size_t i=0; i<mActionList.size(); i++) {
			SpriteSheet& sprite = mActionList[mCurAction].partList[i];
			sprite.setFlipX(mIsFlipX);
			sprite.setCurFrame(mCurFrame);
			sprite.render(x, y);
		}
	}


} // namespace

